function getMoveIn() {
    Ajax({
        url: "http://localhost/Dorm%20Management/server/moveInQueryAll.php",
        success(msg) {
            sessionStorage.setItem("moveInInfo", msg);
            sessionStorage.setItem("currentPage",1)
            msg = JSON.parse(msg).slice(0,10)
            randerMoveIn(msg);
        }
    })
}
getMoveIn()
function queryAll(){
    getMoveIn()
}

function randerMoveIn(msg) {
    const tbody = document.querySelector("tbody");
    tbody.innerHTML = "";
    msg.forEach(item => {
        let str = `
        <tr>
        <td>${item.aname}</td>
        <td>${item.building}</td>
        <td>${item.roomid}</td>
        <td>${item.bedid}</td>
        <td>${item.college}</td>
        <td>${item.clazz}</td>
        <td>${item.gender}</td>
        <td>${item.studentnum}</td>
        <td>${item.phone}</td>
        <td>${item.telphone}</td>
        <td>
          <button type="button" class="btn btn-primary btn-sm" style="padding:5px 15px;" data-bs-toggle="modal"
            data-bs-target="#myModal2" id="${item.aid}" data-add="add">同意</button>
          <button type="button" id="${item.aid}" data-del="del" class="btn btn-danger btn-sm" style="padding:5px 15px;">拒绝</button>
        </td>
      </tr>
      
        `
        tbody.innerHTML += str;
    })
}
const tbody = document.querySelector("tbody")
tbody.onclick = function (e) {
    let login = sessionStorage.getItem("login");
    if(login == '1'){
        return alert('您的权限等级不足！')
    }
    if (e.target.dataset.del == "del") {
        let a = confirm('您确定拒绝申请吗？')   
         if(!a){
             return
         }
        Ajax({
            url: "http://localhost/Dorm%20Management/server/moveInQueryRemove.php",
            data: { id: e.target.id },
            success(msg) {
                alert(msg);
                location.reload();
            }
        })
    }
    if (e.target.dataset.add == "add") {
        let a = confirm('您确定同意该申请吗？')   
         if(!a){
             return
         }
        Ajax({
            url: "http://localhost/Dorm%20Management/server/moveInQueryOne.php",
            data: { id: e.target.id },
            success(msg) {
                msg = JSON.parse(msg)
                Ajax({
                    url: "http://localhost/Dorm%20Management/server/studentQueryAdd.php",
                    data: {
                        id: e.target.id, aname: msg.aname, building: msg.building, roomid: msg.roomid,
                         bedid: msg.bedid,college: msg.college, clazz: msg.clazz, gender: msg.gender, 
                         studentnum: msg.studentnum,phone: msg.phone, telphone: msg.telphone
                    },
                    success(res) {
                        alert(res)
                        Ajax({
                            url: "http://localhost/Dorm%20Management/server/moveInQueryRemove.php",
                            data: { id: e.target.id },
                            success() {
                                location.reload()
                            }
                        })
                    }
                })
            }
        })
    }
}
//分页
function first(){
    let moveInInfo = sessionStorage.getItem('moveInInfo')
    sessionStorage.setItem("currentPage",1)
    moveInInfo = JSON.parse(moveInInfo).slice(0,10)
        randerMoveIn(moveInInfo)
}
function last(){
    let moveInInfo = sessionStorage.getItem('moveInInfo')
    moveInInfo = JSON.parse(moveInInfo)
    sessionStorage.setItem("currentPage",Math.ceil(moveInInfo.length/10))
    console.log(Math.ceil(moveInInfo.length/10));
    if(Math.floor(moveInInfo.length/10)*10 == moveInInfo.length){
     moveInInfo = moveInInfo.slice(Math.floor(moveInInfo.length/10-1)*10,moveInInfo.length)
     }else{
        moveInInfo = moveInInfo.slice(Math.floor(moveInInfo.length/10)*10,moveInInfo.length) 
     }   
     randerMoveIn(moveInInfo)
}
function down(){
    let moveInInfo = sessionStorage.getItem('moveInInfo')
    moveInInfo = JSON.parse(moveInInfo)
    let currentPage = sessionStorage.getItem("currentPage")
    currentPage = +currentPage + 1
    if(currentPage > Math.ceil(moveInInfo.length/10)){
        currentPage = Math.ceil(moveInInfo.length/10)
    }
    sessionStorage.setItem('currentPage',currentPage)
    moveInInfo = moveInInfo.slice((currentPage-1)*10,currentPage*10)
    randerMoveIn(moveInInfo)
}
function up(){
    let moveInInfo = sessionStorage.getItem('moveInInfo')
    moveInInfo = JSON.parse(moveInInfo)
    let currentPage = sessionStorage.getItem("currentPage")
    currentPage = currentPage - 1
    if(currentPage < 1){
        currentPage = 1
    }
    console.log(currentPage);
    sessionStorage.setItem('currentPage',currentPage)
    moveInInfo = moveInInfo.slice((currentPage-1)*10,currentPage*10)
    randerMoveIn(moveInInfo)
}
function likename(){
    const likename = document.querySelector('#likename').value;
    console.log(likename);
    Ajax({
        url:"http://localhost/Dorm%20Management/server/moveInQueryOneAll.php",
        data:{likename},
        success(msg){
            console.log(msg);
            sessionStorage.setItem('moveInInfo',msg)
            sessionStorage.setItem("currentPage",1)
            msg = JSON.parse(msg).slice(0,10)
            
            randerMoveIn(msg)
            // location.reload()
        }
    })
}
